/*
 *  Copyright (C) 2010 Martin Haulrich <mwh.isv@cbs.dk>
 *
 *  This file is part of the MatrixParser package.
 *
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.osdtsystem.matrixparser.parsers;

import java.util.List;
import org.osdtsystem.matrixparser.data.CONLLSentence;
import org.osdtsystem.matrixparser.featureextraction.AbstractFeatureExtractor;
import org.osdtsystem.matrixparser.featureextraction.FirstOrderExtractorLabelled;
import org.osdtsystem.matrixparser.featureextraction.MST2OFeatureExtractorTemporary;
import org.osdtsystem.matrixparser.featureextraction.FirstOrderExtractorUnlabelled;
import org.osdtsystem.matrixparser.features.DefaultMstCache;
import org.osdtsystem.matrixparser.features.FeatureHandler;
import org.osdtsystem.matrixparser.features.FeatureVector;
import org.osdtsystem.matrixparser.features.MstCache;
import org.osdtsystem.matrixparser.features.TwoWayFeatureHandler;
import org.osdtsystem.matrixparser.learners.Scorer;
import org.osdtsystem.matrixparser.parsers.output.DependencyTree;

/**
 * 
 * @author Martin Haulrich
 */
public class LabelledMSTParser2O extends LabelledMSTParser {

    public LabelledMSTParser2O(String name, Scorer scorer, FirstOrderExtractorUnlabelled unlabelledMstExtractor,
            FirstOrderExtractorLabelled labelledMstExtractor, TwoWayFeatureHandler labelAlphabet) {
        super(name, scorer, unlabelledMstExtractor, labelledMstExtractor, labelAlphabet);
    }



    @Override
    List<FeatureVector> featureVectors(DependencyTree tree, List<FeatureVector> aggregator) {
        super.featureVectors(tree, aggregator);
        CONLLSentence sentence = tree.sentence();
        MstCache cache = mstCache(sentence);

        DefaultMstCache defaultMSTCache = (DefaultMstCache) cache;
        CONLLSentence sen = defaultMSTCache.sentence;

        AbstractFeatureExtractor extractor = (AbstractFeatureExtractor) defaultMSTCache.unlabelledExtractor;
        FeatureHandler fh = extractor.featureHandler();

        int instanceLength = tree.nodes();

        for (int node = 0; node < instanceLength; node++) {
            int head = tree.head(node);
            if (head == -1 && node != 0) {
                continue;
            }
            // right children
            int prevChild = node;
            for (int j = node + 1; j < instanceLength; j++) {
                if (tree.head(j) == node) {
                    aggregator.add(MST2OFeatureExtractorTemporary.get2OFeatures(sen, head, j, prevChild, fh));
                    prevChild = j;
                }
            }
            // left children
            prevChild = node;
            for (int j = node - 1; j >= 0; j--) {
                if (tree.head(j) == node) {
                    aggregator.add(MST2OFeatureExtractorTemporary.get2OFeatures(sen, head, j, prevChild, fh));
                    prevChild = j;
                }
            }
        }

        return aggregator;
    }
}
